using System;
using UnityEngine;
using UnityEngine.InputSystem;
using static UnityEngine.InputSystem.InputAction;

namespace Inputs
{
    public class PlayerInput : GameInput<PlayerInput>, GameInputAction.IPlayerActions
    {
        public Action<Vector3> MoveAction;
        public Action<int> ReadySkillAction;
        public Action<int> ReleaseSkillAction;
        public Action<Vector3> SelectAction;

        private void OnEnable()
        {
            InputActions.Player.Enable();
            InputActions.Player.SetCallbacks(this);
        }

        private void OnDisable()
        {
            InputActions.Player.Disable();
        }

        public void OnMove(CallbackContext context)
        {
            var screenPos = Input.mousePosition;
            var ray = Camera.main!.ScreenPointToRay(screenPos);

            Physics.Raycast(ray, out var hit, Mathf.Infinity, LayerMask.GetMask("Ground"));
            MoveAction?.Invoke(hit.point);
        }

        public void OnSelect(CallbackContext context)
        {
            var screenPos = Input.mousePosition;
            var ray = Camera.main!.ScreenPointToRay(screenPos);

            Physics.Raycast(ray, out var hit, Mathf.Infinity, LayerMask.GetMask("Ground"));
            SelectAction?.Invoke(hit.point);
        }

        public void OnQSkill(CallbackContext context)
        {
            OnSKill(context, 0);
        }

        public void OnWSkill(CallbackContext context)
        {
            OnSKill(context, 1);
        }

        public void OnESkill(CallbackContext context)
        {
            OnSKill(context, 2);
        }

        public void OnRSkill(CallbackContext context)
        {
            OnSKill(context, 3);
        }

        private void OnSKill(CallbackContext context, int key)
        {
            if (context.phase == InputActionPhase.Started)
                ReadySkillAction?.Invoke(key);
            else if (context.phase == InputActionPhase.Canceled)
                ReleaseSkillAction?.Invoke(key);
        }
    }
}